package com.yjzx.bean;

import com.yjzx.interfaces.CommonClasses;
import com.yjzx.interfaces.ModifierEnum;
import com.yjzx.util.StringUtils;
import javassist.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @author yjzx
 * @date 2023/10/25
 * @description 构造器
 */
public class UpdateConstructor extends JavassistInfo {
    /**
     * 构造器
     */
    List<Node> params;

    String modifier;

    String body;

    @Override
    public void check() {

    }

    public static class Node {
        String type;
        String name;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

    public List<Node> getParams() {
        return params;
    }

    public void setParams(List<Node> params) {
        this.params = params;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    /**
     * 生成构造器，未考虑父类参数
     *
     * @param pool
     * @param ctClass
     * @return
     */
    public CtConstructor toConstructor(ClassPool pool, CtClass ctClass) {
        CtConstructor constructor = null;
        try {
            boolean flag = true;
            CtClass[] classes = new CtClass[this.params.size()];
            CtField[] declaredFields = ctClass.getDeclaredFields();

            HashMap<String, String> map = new HashMap<>();

            for (CtField declaredField : declaredFields) {
                map.put(declaredField.getName(), declaredField.getType().getName());
            }
            for (int i = 0; i < this.params.size(); i++) {
                Node node = this.params.get(i);
                node.type = CommonClasses.classNameConvert(node.type);
                if (map.containsKey(node.name)) {
                    if (StringUtils.isNotBlank(node.type) && !map.get(node.name).equals(node.type)) {
                        flag = false;
                    }
                } else {
                    flag = false;
                }
                classes[i] = CommonClasses.getCtClass(pool, node.getType());
            }

            constructor = new CtConstructor(classes, ctClass);
            if (flag && StringUtils.isBlank(this.body)) {

                StringBuilder builder = new StringBuilder("{");
                for (int i = 1; i <= this.params.size(); i++) {
                    Node node = this.params.get(i - 1);
                    builder.append("$0.").append(node.name).append("=$").append(i).append(";");
                }
                builder.append("}");
                constructor.setBody(builder.toString());
            } else {
                constructor.setBody(this.body);
            }
            constructor.setModifiers(ModifierEnum.modifierToInt(Collections.singletonList(StringUtils.isBlank(modifier) ? "public" : modifier)));
        } catch (NotFoundException | CannotCompileException e) {
            throw new RuntimeException(e);
        }
        /*"java.lang.Object".equals(ctClass.getSuperclass().getName())*/
        return constructor;
    }
}
